/*
 * 文 件 名:  logger.java
 * 版    权:  Xi'An Leadeon Technologies Co., Ltd. Copyright 2014-4-17,  All rights reserved
 */
package com.atXiao.food.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 类名称: Log
 * 类描述: 日志打印
 *
 * @author: lixiaoxiao
 * 创建时间: 2023/11/23 10:16
 */
public class Log {

    /**
     * 客户端请求交互标识
     */
    public final static String REQ_LOG = "REQ-LOG";

    /**
     * 客户端响应交互标识
     */
    public final static String RSP_LOG = "RSP-LOG";

    /**
     * 客户端响应交互标识
     */
    public final static String AUX_LOG = "AUX-LOG";

    private Logger logger;

    public Log(Class clazz) {
        logger = LogManager.getLogger(clazz);
    }

    public Log(String name) {
        logger = LogManager.getLogger(name);
    }

    public Log(Logger logger) {
        this.logger = logger;
    }

    /**
     * 请求信息打印
     *
     * @param trace   流水号
     * @param busSign 业务分类定义
     * @param busCode 业务编码
     * @param data    请求报文
     */
    public void reqPrint(String trace, String busSign, String busCode, String data) {

        logger.info("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), REQ_LOG, trace, busSign, busCode, data);
    }

    /**
     * 响应信息打印
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param timeUsed
     * @param data
     */
    public void rspPrint(String trace, String busSign, String busCode, Long timeUsed, String data) {
        logger.info("{}|#|{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), RSP_LOG, trace, busSign, busCode, timeUsed, data);
    }

    /**
     * 请求信息打印
     *
     * @param trace   流水号
     * @param busSign 业务分类定义
     * @param busCode 业务编码
     * @param data    请求报文
     */
    public void reqThirdPrint(String trace, String busSign, String busCode, String data) {

        logger.info("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), REQ_LOG, trace, busSign, busCode, data);
    }

    /**
     * 响应信息打印
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param timeUsed
     * @param data
     */
    public void rspThirdPrint(String trace, String busSign, String busCode, int httpCode, Long timeUsed, String data) {
        logger.info("{}|#|{}|#|{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), RSP_LOG, trace, busSign, busCode, httpCode, timeUsed, data);
    }

    /**
     * 业务日志INFO级别信息输出
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param msg
     */
    public void info(String trace, String busSign, String busCode, String msg) {

        logger.info("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), AUX_LOG, trace, busSign, busCode, msg);
    }

    /**
     * 业务日志WARN级别信息输出
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param msg
     */
    public void warn(String trace, String busSign, String busCode, String msg) {

        logger.warn("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), AUX_LOG, trace, busSign, busCode, msg);
    }

    /**
     * 业务日志DEBUG级别信息输出
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param msg
     */
    public void debug(String trace, String busSign, String busCode, String msg) {
        logger.warn("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), AUX_LOG, trace, busSign, busCode, msg);
    }

    /**
     * 业务日志ERROR级别日志输出
     *
     * @param trace
     * @param busSign
     * @param busCode
     * @param msg
     * @param e
     */
    public void error(String trace, String busSign, String busCode, String msg, Exception e) {
        logger.error("{}|#|{}|#|{}|#|{}|#|{}|#|{}", getLineNum(), AUX_LOG, trace, busSign, busCode, msg, e);
    }


    /**
     * 业务统计日志输出
     *
     * @param msg
     */
    public void staticInfo(String msg) {
        logger.info(msg);
    }


    /**
     * 自定义INFO级别信息输出
     *
     * @param msg
     * @param msg
     */
    public void info(String msg) {

        info("", "", "", msg);
    }

    /**
     * 自定义INFO级别信息输出
     */
    public void clickCount(String trace, String serialId, String time) {

        logger.info("{},{},{}", trace, serialId, time);
    }

    /**
     * 自定义WARN级别信息输出
     *
     * @param msg
     */
    public void warn(String msg) {

        warn("", "", "", msg);
    }

    /**
     * 业务日志DEBUG级别信息输出
     *
     * @param msg
     */
    public void debug(String msg) {
        debug("", "", "", msg);
    }

    /**
     * 业务日志ERROR级别日志输出
     *
     * @param msg
     * @param e
     */
    public void error(String msg, Exception e) {
        error("", "", "", msg, e);
    }

    /**
     * 获取行号
     *
     * @return
     */
    public Integer getLineNum() {
        // 读取栈信息获取真正行号
        Throwable throwable = new Throwable();
        StackTraceElement[] ste = throwable.getStackTrace();
        return ste[2].getLineNumber();
    }
}
